﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyBanHang.DAL;

namespace QuanLyBanHang
{
    public partial class Form_SP1 : Form
    {
        bool x=false;
        SanPham_DAL sp = new SanPham_DAL();
        int masp;
        public Form_SP1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        //private void btn_xem_Click(object sender, EventArgs e)
        //{
        //    dataGridView1.DataSource = sp.SelectSP("");
        //}

        private void btn_them_Click(object sender, EventArgs e)
        {

            dataGridView1.DataSource = sp.SelectSP("");
            x = true;
            //btn_sua.Enabled = false;
            //panel1.Enabled = false;
           // btn_timkiem.Enabled = false;
       //     btn_xem.Enabled = false;
            //btn_xoa.Enabled = false;
            //btn_them.Enabled = true;
            btn_luu.Enabled = true;
            btn_huy.Enabled = true;
            txt_masanpham.Enabled = false;
            panel2.Enabled = true;
            panel3.Enabled = false;

        }

        private void btn_luu_Click(object sender, EventArgs e)
        {
            try
            {
                if (x == true)
                {
                    sp.insertSP(txt_tensanpham.Text, Convert.ToInt32(txt_soluong.Text), Convert.ToInt32(txt_gia.Text), txt_loaisanpham.Text, txt_mota.Text);
                    dataGridView1.DataSource = sp.SelectSP("");
                    btn_them.Enabled = true;
                    btn_thoat.Enabled = true;
                    btn_timkiem.Enabled = true;
                    //       btn_xem.Enabled = true;
                    btn_xoa.Enabled = true;
                    btn_sua.Enabled = true;
                    btn_luu.Enabled = false;
                    btn_huy.Enabled = false;
                    panel2.Enabled = false;
                    panel3.Enabled = true;
                }
                else
                {
                    sp.updateSP(Convert.ToInt32(txt_masanpham.Text), txt_tensanpham.Text, Convert.ToInt32(txt_soluong.Text), Convert.ToInt32(txt_gia.Text), txt_loaisanpham.Text, txt_mota.Text);
                    dataGridView1.DataSource = sp.SelectSP("");
                    btn_them.Enabled = true;
                    btn_thoat.Enabled = true;
                    btn_timkiem.Enabled = true;
                    //     btn_xem.Enabled = true;
                    btn_xoa.Enabled = true;
                    btn_sua.Enabled = true;
                    btn_luu.Enabled = false;
                    btn_huy.Enabled = false;
                    panel2.Enabled = false;
                    panel3.Enabled = true;
                }
            }
            catch
            {
                MessageBox.Show("Có lỗi !!!");
            }

            dataGridView1.DataSource = sp.SelectSP("");
        }

        private void btn_sua_Click(object sender, EventArgs e)
        {
            try
            {
                x = false;
                panel2.Enabled = true;
                txt_masanpham.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                txt_tensanpham.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                txt_soluong.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                txt_gia.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                txt_loaisanpham.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                txt_mota.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
             //   btn_them.Enabled = false;
             ////   btn_xem.Enabled = false;
             //   btn_xoa.Enabled = false;
             //   btn_sua.Enabled = true;
                btn_luu.Enabled = true;
                btn_huy.Enabled = true;
                //dataGridView1.ReadOnly = false;
                //dataGridView1.Columns[0].ReadOnly = true;
                panel3.Enabled = false;
                masp = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            }
            catch (Exception a)
            {
                MessageBox.Show("Chọn Hàng Cần Sửa");
            }
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                if (sp.deleteSP(id)) MessageBox.Show("Đã xóa");
                else MessageBox.Show("Có lỗi");
                dataGridView1.DataSource = sp.SelectSP(txt_timkiem.Text);
            }
            catch(Exception gg) 
            {
                MessageBox.Show("Chọn Hàng Cần Xóa");
            }
        }

        private void btn_timkiem_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = sp.SelectSP(txt_timkiem.Text);
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btn_huy_Click(object sender, EventArgs e)
        {
            panel1.Enabled = true;
            panel2.Enabled = false;
            panel3.Enabled = true;
            btn_huy.Enabled = false;
            btn_luu.Enabled = false;
          //  btn_them.Enabled = true;
        //    btn_xem.Enabled = true;
            //btn_xoa.Enabled = true;
            //btn_sua.Enabled = true;
            //btn_luu.Enabled = false;
            //btn_thoat.Enabled = true;
            //btn_luu.Enabled = false;
            //btn_huy.Enabled = false;
        }

        private void lal_loaisanpham_Click(object sender, EventArgs e)
        {

        }

        private void lal_tensanpham_Click(object sender, EventArgs e)
        {

        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void lal_soluong_Click(object sender, EventArgs e)
        {

        }

        private void txt_soluong_TextChanged(object sender, EventArgs e)
        {

        }

        private void lal_gia_Click(object sender, EventArgs e)
        {

        }

        private void txt_tensanpham_TextChanged(object sender, EventArgs e)
        {

        }

        private void txt_masanpham_TextChanged(object sender, EventArgs e)
        {

        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form_SP1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = sp.SelectSP("");
        }
    }
}
